package com.ty.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ty.entity.SysAdmin;
import com.ty.entity.SysRole;
import com.ty.service.AdminService;
import com.ty.service.RoleService;
import com.ty.service.UserRoleService;
import com.ty.util.Pager;

@Controller
@RequestMapping("/admin/user")
public class UserController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private RoleService roleService;
	
	
	//1.首页
	@RequiresPermissions("admin:user:index")
	@RequestMapping("/index")
	public String index() {
		return "admin/user/index";
	}
	
	
	//2.列表页
	@RequiresPermissions("admin:user:indexList")
	@RequestMapping("/indexList")
	public String indexList(int pageNum,int pageSize,SysAdmin admin,ModelMap model) {
		Pager pager= adminService.selectByPager(pageNum, pageSize, admin);
		model.put("pager", pager);
		return "admin/user/indexList";
	} 
	
	//3.单个删除
	@RequiresPermissions("admin:user:deleteOne")
	@RequestMapping("/deleteOne")
	@ResponseBody
	public Map<String,Object> deleteOne(Long id){
		boolean flag= adminService.deleteById(id);
		Map<String, Object> returnJson=new HashMap<>();
		if(flag) {
			returnJson.put("status", 1);
			returnJson.put("msg","操作成功！");
		}else {
			returnJson.put("status", 0);
			returnJson.put("msg","操作失败！");
		}
		return returnJson;
	}
	
	//4.多个删除
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Map<String,Object> batchDelete(String ids){
		boolean flag= adminService.deleteBatch(ids);
		Map<String, Object> returnJson=new HashMap<>();
		if(flag) {
			returnJson.put("status", 1);
			returnJson.put("msg","操作成功！");
		}else {
			returnJson.put("status", 0);
			returnJson.put("msg","操作失败！");
		}
		return returnJson;
	}
	//5.保存或更新
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Map<String, Object> saveOrUpdate(SysAdmin admin,int isUpdate){
		 boolean flag=false;
		 if(isUpdate==1) { //更新操作
			 flag=adminService.updateByObj(admin);
		 }else {  //保存操作
			 flag=adminService.saveObject(admin);
		 }
		 Map<String, Object> returnJson=new HashMap<>();
		 if(flag) {
			 returnJson.put("status", "1"); //操作成功
			 returnJson.put("msg", "操作成功！");
		 }else {
			 returnJson.put("status", "0"); //操作失败
			 returnJson.put("msg", "操作失败！");
		 }
		 return returnJson;
		
	}
	
	//6.跳转到更新或保存页面
	@RequestMapping("/get")
	public String get(Long id,ModelMap model) {
		if(id!=null) { //调整到更新
			SysAdmin admin= adminService.getById(id);
			model.put("m",admin);
			model.put("isUpdate", 1); //标记当前页面是更新
		}else {
			model.put("isUpdate", 0);//标记当前页面是保存
		}
		
		return "admin/user/saveOrUpdate";
	}
	
	//验证的方法
	@RequestMapping("/valid")
	@ResponseBody
	public Map<String,Object> valid(String param) {
		SysAdmin admin= adminService.getByName(param);
		Map<String,Object> returnJson=new HashMap<>();
		if(admin==null) { //表示当前用户名可以正常使用
			returnJson.put("status", "y");
			returnJson.put("info","当前用户名可以正常使用!");
		}else { //表示当前用户名已存在，请重新输入
			returnJson.put("status", "n");
			returnJson.put("info","当前用户名已存在，请重新输入!");
		}
		return returnJson;
	}
	
	/**
	 * 授权
	 * @return
	 */
	@RequestMapping("/showUserRole")
	public String showUserRole(Long userId,ModelMap model) {
		SysAdmin sysAdmin= adminService.getById(userId);
		List<SysRole> list= roleService.selectAll(userId);
		
		model.put("m",sysAdmin);
		model.put("list",list);
		return "admin/user/showUserRole";
	}
	
	@Autowired
	private UserRoleService userRoleService;
	
	/**
	 * 保存授权信息
	 * @return
	 */
	@RequestMapping("/saveUserRole")
	@ResponseBody
	public Map<String,Object> saveUserRole(Long userId,Long []roleId) {
		Map<String,Object> returnJson=new HashMap<>();
		
		boolean flag= userRoleService.batchUserRole(userId, roleId);
		 if(flag) {
			 returnJson.put("status", "1"); //操作成功
			 returnJson.put("msg", "操作成功！");
		 }else {
			 returnJson.put("status", "0"); //操作失败
			 returnJson.put("msg", "操作失败！");
		 }
		 return returnJson;
	}
}
